package com.power.home.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.power.home.R;
import com.power.home.common.Constant;
import com.power.home.common.util.StringUtil;
import com.power.home.common.util.UIUtil;
import com.power.home.data.bean.TeamInfoBean;
import com.power.home.di.component.AppComponent;
import com.power.home.di.component.DaggerTeamComponent;
import com.power.home.di.module.TeamInfoModule;
import com.power.home.presenter.TeamInfoPresenter;
import com.power.home.presenter.contract.TeamInfoContract;
import com.power.home.ui.adapter.VipRecordAdapter;
import com.power.home.ui.widget.MyTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhp on 2019-11-08
 */

@Route(path = "/android/myTeam")
public class TeamActivity extends BaseActivity<TeamInfoPresenter> implements TeamInfoContract.View, View.OnClickListener {


    @BindView(R.id.titleBar)
    MyTitleBar titleBar;
    @BindView(R.id.tv_team_all_number)
    TextView tvTeamAllNumber;
    @BindView(R.id.tv_team_use_number)
    TextView tvTeamUseNumber;
    @BindView(R.id.tv_team_surplus_number)
    TextView tvTeamSurplusNumber;

    @BindView(R.id.rc_person)
    RecyclerView recyclerView;
    FriendAdapter friendAdapter;
//    @BindView(R.id.tv_invite_vip_number)
//    TextView tvInviteVipNumber;
//    @BindView(R.id.ll_invite_vip)
//    LinearLayout llInviteVip;
//    @BindView(R.id.tv_invite_ambassador_number)
//    TextView tvInviteAmbassadorNumber;
//    @BindView(R.id.ll_invite_ambassador)
//    LinearLayout llInviteAmbassador;
//    @BindView(R.id.tv_invite_school_number)
//    TextView tvInviteSchoolNumber;
//    @BindView(R.id.ll_invite_school)
//    LinearLayout llInviteSchool;
    private String queryType;

    List<PersonInfoContainer> infoList = new ArrayList<>();

    @Override
    public int setLayout() {
        return R.layout.activity_team;
    }

    @Override
    public void getExtras() {
        titleBar.setRightLayoutClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                WebViewActivity.launcher(UIUtil.getContext(), "http://www.image.yshili.com/23456780753.jpg", "用户协议");
            }
        });
        queryType = getIntent().getStringExtra(Constant.type);
        if (StringUtil.isNotNullString(queryType)) {
            switch (queryType) {
                case "SCHOOLMASTER"://联盟校长
//                    llInviteAmbassador.setVisibility(View.VISIBLE);
                    break;
                case "HEADQUARTERS"://总部
//                    llInviteAmbassador.setVisibility(View.VISIBLE);
//                    llInviteSchool.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Override
    public boolean init() {
        mPresenter.getTeamInfo();
        initView();
        tvTeamUseNumber.setOnClickListener(this);
        tvTeamSurplusNumber.setOnClickListener(this);

        return false;
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(UIUtil.getContext(), LinearLayoutManager.VERTICAL, false){
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        friendAdapter = new FriendAdapter();
        friendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PersonInfoContainer personInfoContainer = infoList.get(position);
                ARouter.getInstance().build("/android/invitedRecord")
                        .withString(Constant.type, personInfoContainer.type)
                        .navigation();
            }
        });
        recyclerView.setAdapter(friendAdapter);
    }

    @Override
    public void setListener() {
        backListener(titleBar);
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {
        DaggerTeamComponent.builder().appComponent(appComponent)
                .teamInfoModule(new TeamInfoModule(this))
                .build().inject(this);
    }

    @Override
    public void getTeamInfoSuccess(TeamInfoBean teamInfoBean) {
        if (null != teamInfoBean) {
            tvTeamAllNumber.setText(teamInfoBean.getTeamTotalCount());
            tvTeamUseNumber.setText("已用名额:" + teamInfoBean.getUsedQuota());
            tvTeamSurplusNumber.setText("未用名额:" + teamInfoBean.getSurplusQuota());
            int type;
            // 该身份只能查看上一级数据  AMBASSADOR--->只能看VIP
            switch (queryType) {
                case "AMBASSADOR":
                    infoList.add( this.createPersonInfo("VIP","我的会员",teamInfoBean.getMyVip().getCount()));
                    infoList.add(this.createPersonInfo("NORMAL","我的好友",teamInfoBean.getNormal().getCount()));

//
                    break;
                case "SCHOOLMASTER":
                case "HEADQUARTERS":
                    infoList.add( this.createPersonInfo("VIP","我的会员",teamInfoBean.getMyVip().getCount()));
                    infoList.add(this.createPersonInfo("NORMAL","我的好友",teamInfoBean.getNormal().getCount()));
                    infoList.add( this.createPersonInfo("AMBASSADOR","我的联盟校长",teamInfoBean.getMyAmbassador().getCount()));
                    infoList.add(this.createPersonInfo("SCHOOLMASTER","我的同盟校长",teamInfoBean.getMySchoolMaster().getCount()));
                    break;
                default:

                    break;
            }
        }
        friendAdapter.notifyDataSetChanged();
    }

    private PersonInfoContainer createPersonInfo(String type,String title,String count){
         PersonInfoContainer personInfoContainer = new PersonInfoContainer();
         personInfoContainer.setType(type);
         personInfoContainer.setTitle(title);
         personInfoContainer.setCount(count);
         return personInfoContainer;
    }

    @Override
    public void onClick(View view) {
        boolean useStatus = false;
        switch (view.getId()) {

            case R.id.tv_team_use_number:


                break;
            case R.id.tv_team_surplus_number:
                useStatus = true;
                break;

        }
        ARouter.getInstance().build("/android/codeConvertList")
                .withBoolean(Constant.id, useStatus)
                .navigation();

    }
    public class PersonInfoContainer{

        private String type;
        private String title;
        private String count;

        public void setTitle(String title){
            this.title = title;
        }
        public String getTitle(){

            return this.title;
        }

        public void setCount(String count){
            this.count = count;


        }

        public String getCount(){
            return this.count;
        }

        public void setType(String type){
            this.type = type;
        }

        public String getType(){

            return this.type;
        }
    }

    public class FriendAdapter extends BaseQuickAdapter<PersonInfoContainer, BaseViewHolder>{

        public  FriendAdapter(){
            super(R.layout.team_main_item,infoList);
        }
        @Override
        protected void convert(BaseViewHolder helper, PersonInfoContainer item) {
             TextView tv_title = helper.getView(R.id.tv_title);
             TextView tv_count = helper.getView(R.id.tv_number);
             tv_title.setText(item.getTitle());
             tv_count.setText(item.getCount());

        }
    }
}
